package com.lx.idea.report.config;

import cn.hutool.core.util.XmlUtil;
import com.lx.idea.common.Constant;
import com.lx.idea.report.model.InstitutionRegisterInfo;
import com.lx.idea.report.model.RootRealTimeInfo;
import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @Author ybb
 * @Description
 * @Date 2021/9/16 17:55
 */
@Slf4j
public class RealTimeReport {


    private  Document document = null;
    public RealTimeReport(){
        document = XmlUtil.createXml();
    }

    /*
      设置各个节点的元素属性
     */
    public  Document genByNodeLevel(int level, InstitutionRegisterInfo info, RootRealTimeInfo e,Map map)  {
        try {
            Element root = setRootAttribute(level);
            int index = level;
            Element pubKeySign = document.createElement(Tag.sign[index]);
            pubKeySign.setTextContent("fdffg");
            Element regManComInfo = setComInfo(info, level, map);
            Element data = document.createElement(Tag.data[index]);
            data = setRealTime(data, Tag.text[index], e);

            root.appendChild(pubKeySign);
            root.appendChild(regManComInfo);
            root.appendChild(data);
            document.appendChild(root);
        }catch (Exception ex){
            log.error("Document 对象生成出错 => level={}",level);
            ex.printStackTrace();
            return null;
        }
        return document;
    }


    /*
      生成头部属性
     */
    public  Element setRootAttribute(int level){
        Element root = null;
        switch (level) {
            case Constant.level_root:
                root = document.createElement("rrti:rootReaTimInfo");
                root.setAttribute("xmlns:rrti", "urn:ietf:params:xml:ns:rootreatiminfo-1.0");
                root.setAttribute("xmlns:rsrti", "urn:ietf:params:xml:ns:regsrvreatiminfo-1.0");
                root.setAttribute("xmlns:rmci", "urn:ietf:params:xml:ns:rootservcominfo-1.0");
                root.setAttribute("xmlns:bt", "urn:ietf:params:xml:ns:basetype-1.0");
                break;
                //      case Constant.level_1://管理节点暂且不需要
            case Constant.level_2:
                root = document.createElement("rsrti:regSrvReaTimInfo");
                root.setAttribute("xmlns:rsrti", "urn:ietf:params:xml:ns:regsrvreatiminfo-1.0");
                root.setAttribute("xmlns:bt", "urn:ietf:params:xml:ns:basetype-1.0");
                root.setAttribute("xmlns:rmci", "urn:ietf:params:xml:ns:regservcominfo-1.0");
                break;
            case Constant.level_3:
                root = document.createElement("erti:entReaTimInfo");
                root.setAttribute("xmlns:erti", "urn:ietf:params:xml:ns:entreatiminfo-1.0");
                root.setAttribute("xmlns:bt", "urn:ietf:params:xml:ns:basetype-1.0");
                root.setAttribute("xmlns:eci", "urn:ietf:params:xml:ns:entcominfo-1.0");
                root.setAttribute("xmlns:rsrti", "urn:ietf:params:xml:ns:regsrvreatiminfo-1.0");
                break;
        }
        return root;
    }

    public  Element setComInfo(InstitutionRegisterInfo info,int level,Map map){

        int index = level;
        Element regManComInfo = document.createElement(Tag.comInfo[index]);

        Element regServerType = document.createElement(Tag.serverTYpe[index]);
        Element servName =   document.createElement(Tag.id[index]+"servName");
        Element gnrlId = document.createElement(Tag.id[index]+"gnrlId");
        servName.setTextContent(info.getServName());
        gnrlId.setTextContent(info.getGnrlId());
        if (Constant.level_3 == level){
            Element serverName = document.createElement(Tag.serverName);
            serverName.appendChild(servName);
            serverName.appendChild(gnrlId);
            regServerType.appendChild(serverName);
        }else {
            regServerType.appendChild(servName);
            regServerType.appendChild(gnrlId);
        }
        Element idType = document.createElement(Tag.comTag[index]+"idType");
        idType.setTextContent(info.getIdType());
        Element dataCrtTime = document.createElement(Tag.comTag[index]+"dataCrtTime");
        dataCrtTime.setTextContent(info.getDataCrtTime().toString());
        Element operType = document.createElement(Tag.comTag[index]+"operType");
        operType.setTextContent(info.getOperType());
        Element transId = document.createElement(Tag.comTag[index]+"transId");
        transId.setTextContent(info.getTransId().toString());

        //企业上报需要增加上级机构的属性
        if (Constant.level_3 == level){
            Element ower = document.createElement("eci:ownerRegServ");
            map.keySet().forEach(key -> {
                Element owerServer =   document.createElement(key.toString());
                owerServer.setTextContent(map.get(key).toString());
                ower.appendChild(owerServer);
            });
            regManComInfo.appendChild(ower);
        }
        if (Constant.level_3!= level){
            regServerType.appendChild(idType);
            regServerType.appendChild(transId);
        }
        regServerType.appendChild(dataCrtTime);
        regServerType.appendChild(operType);

        regManComInfo.appendChild(regServerType);

        return regManComInfo;
    }

    /*
     生成上报内容
     */
    public  Element setRealTime(Element data,String tag,RootRealTimeInfo T){
        try {
            Field[] fields = T.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field f = fields[i];
                f.setAccessible(true);
                Element el = document.createElement(tag + f.getName());
                Object obj = f.get(T);
                el.setTextContent(null == obj ? "" : obj.toString());
                data.appendChild(el);
            }
        }catch (Exception e){
            log.error("内容生成失败 => {}",T.getClass().getName());
            e.printStackTrace();
        }
        return data;
    }
}
